// pages/collectMoney/collectMoney.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money:"",
    remark:"",
    remarkLength:0,
    showCode:false,
    orgMoney:0,
    phone:"",
    time: 60, //倒计时 
    currentTime: 60,
    disabled:false,
    code:"",
    ismodal:false,
    stateText:"",
    stateLittle:"",
    isshow:false
  },
  //设置数据
  toggle: function (type, val) {
    this.setData({
      [type]: val
    });
  },
  isshowbtn(){
    this.toggle("isshow", true);
  },
  textblur(){
    this.toggle("isshow", false);
  },
  moneyInput(e){
    e.detail.value = e.detail.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    e.detail.value = e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');

    if (e.detail.value.indexOf(".") < 0 && e.detail.value != "") {
      e.detail.value = parseFloat(e.detail.value);
    } else if (e.detail.value.indexOf(".") == 0) {
      console.log(1111111111111111111111111111)
      e.detail.value = e.detail.value.replace(/[^$#$]/g, "0.");
      e.detail.value = e.detail.value.replace(/\.{2,}/g, ".");
    }
    if (e.detail.value > this.data.orgMoney) {
      e.detail.value = this.data.orgMoney
    }
    this.toggle("money", e.detail.value);
    
    if (e.detail.value.trim() && this.data.code){
      this.toggle("disabled", true);
    } else {
      this.toggle("disabled", false);
    }
  },
  remarkTextarea(e){
    this.toggle("remark", e.detail.value.trim());
    this.toggle("remarkLength", e.detail.value.trim().length);
    if (e.detail.value.trim() && this.data.code && this.data.money) {
      this.toggle("disabled", true);
    }else{
      this.toggle("disabled", false);
    }
  },
  // 全部提现
  tokenAllMoney(){
    this.toggle("money", this.data.orgMoney);
  },
  daleteMoney(){
    this.toggle("money", "");
  },

  getCode(){
    var that = this;
    var currentTime = that.data.currentTime
    var interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: 60,
          currentTime: 60,
          showCode: false
        })
      }
    }, 1000)
  },
  // 获取验证码
  getVerificationCode() {
    var that = this;
    app.req({
      url: "orgmini/sys/sendCode?telphone=" + that.data.userInfo.telphone + "&type=3",
      success(res){
        if(res.code == 200){
          wx.showToast({
            title: "发送成功",
            icon: 'none',
            duration: 2000
          })
          that.getCode();
          that.toggle("showCode", true);
        }else{
          wx.showToast({
            title: "发送失败",
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  codeInput(e){
    this.toggle("code", e.detail.value.trim());
    if (e.detail.value.trim() && this.data.money) {
      this.toggle("disabled", true);
    }else{
      this.toggle("disabled", false);
    }
  },
  submit(){
    if(!this.data.money){
      wx.showToast({
        title:'请输入提现金额',
        icon: 'none',
        duration: 2000
      })
      return
    } else if (!this.data.code){
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    var that = this;
    if (!this.data.disabled){
      return
    }
    app.req({
      url:"orgmini/orgAccount/orgAccountInitiation",
      method:"post",
      data:{
        orgId: that.data.orgId,
        code: that.data.code,
        applyCashMoney: that.data.money,
        applyExplain: that.data.remark
      },
      success(res){
        if (res.code == 200){
          console.log(res)
          that.toggle("ismodal", true);
          if (res.data.isCheck == 1){
            that.toggle("stateText", "提交审核");
            that.toggle("stateLittle", "您的提现申请已提交 请耐心等待");
          }else{
            that.toggle("stateText", "提现成功");
            that.toggle("stateLittle", "您的提现已到账 可前往我的钱包查看");
          }
        }
      }
    })
  },
  preventTouchMove() {},
  cancel(){
    this.toggle("ismodal", false);
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.toggle("orgMoney", options.orgMoney);
    this.toggle("orgId", options.orgId);
    // this.toggle("orgMoney", 50.2);
    // this.toggle("orgId", "123d8a1d1cc511ea995200163e05ff66");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.getUserInfo(function (userInfo) {
      that.toggle("userInfo", userInfo);
      var tel = userInfo.telphone.substring(0, 3) + "****" + userInfo.telphone.substring(7, 11);
      that.toggle("phone", tel);
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})